﻿//初始化Form1
function doOnFormInit1(){
	if (typeof(editForm1) == "object"){
		editForm1.attachEvent("onButtonClick", function(name, command) {
			switch(command) {
				case 'save':doSave();break;
				case 'cancle':doCancle();break;
			}
		});
//		editForm1.disableItem("oldPassword");
//		editForm1.disableItem("oldPasswordTag");
//		editForm1.disableItem("passwordtag");
//		editForm1.disableItem("confirm_passwordtag");
	}
	$("input[name='password']").focus(function () {
		$("input[name='passwordtag']").css({color:"#666666"});
		editForm1.setItemValue("passwordtag","10~20位（字母/数字）");
	});
	$("input[name='password']").blur(function () {
		var password=$.trim($(this).val());
		var password_lng=password.length;
		editForm1.setItemValue("passwordtag","");
		$("input[name='passwordtag']").css({color:"red"});
		if(password.length==0){
			editForm1.setItemValue("passwordtag","请输入登录密码");
		}else if(password_lng<6||password_lng>20){
			editForm1.setItemValue("passwordtag","10~20位（字母/数字）");
		}
	});
	$("input[name='confirm_password']").focus(function () {
		$("input[name='confirm_passwordtag']").css({color:"#666666"});
		editForm1.setItemValue("confirm_passwordtag","10~20位（字母/数字）");
	});
	$("input[name='confirm_password']").blur(function () {
		var password=$.trim(editForm1.getItemValue("password"));
		var rePassword=$.trim($(this).val());
		var rePassword_lng=password.length;
		editForm1.setItemValue("confirm_passwordtag","");
		$("input[name='confirm_passwordtag']").css({color:"red"});
		if(rePassword_lng.length==0){
			editForm1.setItemValue("confirm_passwordtag","请输入确认密码");
		}else if(rePassword_lng<6||rePassword_lng>20){
			editForm1.setItemValue("confirm_passwordtag","10~20位（字母/数字）");
		}else if(password!=rePassword){
			editForm1.setItemValue("confirm_passwordtag","两次输入密码不一致");
		}
	});
    $("input[name='password']").on("paste",function(){
        return false;
    });
    $("input[name='password']").on("drop",function(){
        return false;
    });
    $("input[name='password']").on("contextmenu",function(){
        return false;
    });

}

//校验新增/编辑form表单
function checkForm(){
	$("input[name$='tag']").val("");
	var oldPassword=$.trim(editForm1.getItemValue("oldPassword"));
	var password=$.trim(editForm1.getItemValue("password"));
	var repassword=$.trim(editForm1.getItemValue("confirm_password"));
	if(oldPassword.length==0){
		$func.alert('请输入原密码！', 'red');
		return false;
	}
    if(password.length==0){
        $func.alert('请输入新密码！', 'red');
        return false;
    }
	if(repassword.length==0){
		$func.alert('请输入确认密码！', 'red');
		return false;
	}
    //if( password.isCn() ){
    //    $func.alert('不能包含中文!', 'red');
    //    return false;
    //}
	if((password.length<10||password.length>20)){
		$func.alert('修改密码必须10~20位', 'red');
		return false;
	}
	if(!password.isPwd2()){
		$func.alert('登录密码必须是大小写字母和数字', 'red');
		return false;
	}
	if(password != repassword){
		$func.alert('新密码和确认密码不一致，请重新输入', 'red');
		return false;
	}

	return true;
}
//保存动作
function doSave(){
	if(!checkForm())return;
	var formData = editForm1.getFormData();
	var postData="password="+$.md5(formData.confirm_password).toUpperCase()+"&oldPassword="+ $.md5(formData.oldPassword).toUpperCase();
	$.ajax({
	    url: basePath+'changePwd',
        type: 'post',
        dataType: 'json',
        data:postData,
        success: function (back) {
        	if(!back){
        		$func.alert("修改密码失败！", "red");
        		return;
        	}
        	if(!back.result){
        		$func.alert(back.message?back.message:'修改失败' , "red");
        		return;
        	}
        	$func.alert("修改密码成功！", "green");
        	doCancle();
        }
    });
}
//取消动作
function doCancle(){
	parent.changePwdWin.close();
}
